package com.zhao.rabbitmq.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author xiaoZhao
 * @date 2022/9/26
 * @describe 消费者
 */
public class Consumer {

    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 工厂IP
        factory.setHost("139.196.177.161");
        //  用户名
        factory.setUsername("admin");
        factory.setPassword("123");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 消费消息
        // 声明 接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            System.out.println(new String(message.getBody()));
        };
        // 取消消息回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消息消费中断");
        };
        /**
        * 1.消费那个队列
         * 2.消费成功之后是否要自动应答 true 自动应答 false 手动应答
         * 3.消费者未成功消费的回调
         * 4.消费者取消消费的回调
        **/
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
